package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.bo.company.CompanyAPPInfo;
import com.dmap.knowledge.controller.api.param.ArticleColumnAppEditParam;
import com.dmap.knowledge.controller.api.param.ArticleColumnAppParam;
import com.dmap.knowledge.dao.ArticleColumnAppMapper;
import com.dmap.knowledge.po.ArticleColumn;
import com.dmap.knowledge.po.ArticleColumnApp;
import com.dmap.knowledge.po.ArticleColumnAppExample;
import com.dmap.knowledge.rpc.CompanyServiceRpc;
import com.dmap.knowledge.service.ArticleColumnAppExtensionService;
import com.dmap.knowledge.service.ArticleColumnAppService;
import com.dmap.knowledge.service.ArticleColumnService;
import com.dmap.knowledge.service.ArticleExtensionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ArticleColumnAppServiceImpl implements ArticleColumnAppService {
    @Resource
    private ArticleColumnService articleColumnService;
    @Resource
    private ArticleColumnAppMapper articleColumnAppMapper;
    @Resource
    private CompanyServiceRpc companyServiceRpc;
    @Resource
    private ArticleExtensionService articleExtensionService;
    @Resource
    private ArticleColumnAppExtensionService articleColumnAppExtensionService;

    @Override
    public List<ArticleColumnApp> selectArticleColumnAppAll(String projectId) {
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria articleColumnAppCriteria = articleColumnAppExample.createCriteria();
        //status 为1时，进行上架
        articleColumnAppCriteria.andStatusEqualTo(1);
        articleColumnAppCriteria.andAppNumberEqualTo(projectId);
        //按照降序
        articleColumnAppExample.setOrderByClause("sort asc,create_time desc");
        return articleColumnAppMapper.selectByExample(articleColumnAppExample);
    }

    @Override
    public List<ArticleColumnApp> selectAppArticleColumnAppAll(String lang, String projectId) {
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria articleColumnAppCriteria = articleColumnAppExample.createCriteria();
        //status 为1时，进行上架
        articleColumnAppCriteria.andStatusEqualTo(1);
        articleColumnAppCriteria.andAppNumberEqualTo(projectId);
        articleColumnAppCriteria.andLangEqualTo(lang);
        //columnId 不启动不行
        List<ArticleColumn> articleColumnList = articleColumnService.selectArticleColumnNotOn();
        List<Long> articleColumnIds = new ArrayList<>();
        articleColumnList.forEach(articleColumn -> {
            articleColumnIds.add(articleColumn.getId());
        });
        if (CollectionUtils.isNotEmpty(articleColumnIds)) {
            articleColumnAppCriteria.andArticleColumnIdNotIn(articleColumnIds);
        }
        //按照降序
        articleColumnAppExample.setOrderByClause("sort asc,create_time desc");
        return articleColumnAppMapper.selectByExample(articleColumnAppExample);
    }

    @Override
    public int countStatusColumnApp(String lang, String appNumber, long columnId) {
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        if (columnId != 0) {
            criteria.andArticleColumnIdEqualTo(columnId);
        }
        //启用
        criteria.andStatusEqualTo(1);
        int count = articleColumnAppMapper.countByExample(articleColumnAppExample);
        if (count > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<ArticleColumnApp> selectColumnAppNotOn(String lang, String appNumber, long columnId) {
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        if (columnId != 0) {
            criteria.andArticleColumnIdEqualTo(columnId);
        }
        //选择没有启用的
        criteria.andStatusNotEqualTo(1);
        return articleColumnAppMapper.selectByExample(articleColumnAppExample);
    }

    @Override
    public int insertArticleColumnApp(ArticleColumnAppParam param) {
        //如果不存在就返回插入失败
        if (articleColumnService.selectArticleColumnById(Long.parseLong(param.getArticleColumnId())) == null) {
            return 0;
        }
        //唯一性验证
        if (articleColumnAppExtensionService.checkColumnAppList(param.getLang(), param.getAppNumber(), param.getArticleColumnId(), null) != 0) {
            return 2;
        }
        ArticleColumnApp articleColumnApp = new ArticleColumnApp();
        articleColumnApp.setLang(param.getLang().toUpperCase());
        //设置文章栏目app,公司信息
        CompanyAPPInfo companyAPPInfo = companyServiceRpc.selectCompanyAppInfo(param.getAppNumber());
        articleColumnApp.setCompanyName(companyAPPInfo.getCompanyName());
        articleColumnApp.setAppNumber(param.getAppNumber());
        articleColumnApp.setAppName(param.getAppName());
        articleColumnApp.setAppId(companyAPPInfo.getAppId());
        articleColumnApp.setCompanyId(companyAPPInfo.getCompanyId());
        articleColumnApp.setCompanyNumber(companyAPPInfo.getCompanyNumber());
        articleColumnApp.setCompanyId(companyAPPInfo.getCompanyId());
        articleColumnApp.setCompanyName(companyAPPInfo.getCompanyName());
        //设置文章，栏目基本信息
        articleColumnApp.setArticleColumnId(Long.parseLong(param.getArticleColumnId()));
        articleColumnApp.setColumnName(param.getColumnName());
        articleColumnApp.setDescription(param.getDescription());
        articleColumnApp.setSort(param.getSort());
        articleColumnApp.setCreateTime(System.currentTimeMillis() / 1000);
        articleColumnApp.setUpdateTime(System.currentTimeMillis() / 1000);
        articleColumnApp.setStatus(param.getStatus());
        articleColumnApp.setSort(param.getSort());
        int success = articleColumnAppMapper.insertSelective(articleColumnApp);
        //修改热门文章缓存
        articleExtensionService.updateHotArticleCachePo(articleColumnApp.getLang(),articleColumnApp.getAppNumber());
        //修改今日文章推荐缓存
        articleExtensionService.updateTodayArticleCachePo(articleColumnApp.getLang(),articleColumnApp.getAppNumber());
        return success;
    }

    @Override
    public int deleteArticleColumnApp(long articleColumnAppId) {
        ArticleColumnApp articleColumnAppOne = articleColumnAppMapper.selectByPrimaryKey(articleColumnAppId);
        if (articleColumnAppOne == null) {
            return 0;
        }
        if (articleExtensionService.countArticleByColumn(articleColumnAppOne.getAppNumber(), articleColumnAppOne.getArticleColumnId()) != 0) {
            return 0;
        }
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        articleColumnAppExample.createCriteria().andIdEqualTo(articleColumnAppId);
        int success =  articleColumnAppMapper.deleteByExample(articleColumnAppExample);
        //修改热门文章缓存
        articleExtensionService.updateHotArticleCachePo(articleColumnAppOne.getLang(),articleColumnAppOne.getAppNumber());
        //修改今日文章推荐缓存
        articleExtensionService.updateTodayArticleCachePo(articleColumnAppOne.getLang(),articleColumnAppOne.getAppNumber());
        return success;
    }

    @Override
    public int updateArticleColumnApp(ArticleColumnAppEditParam param) {
        ArticleColumnApp articleColumnApp = new ArticleColumnApp();
        ArticleColumnApp articleColumnAppOne = articleColumnAppMapper.selectByPrimaryKey(Long.parseLong(param.getColumnAppId()));
        //如果未空就不允许删除
        if (articleColumnAppOne == null) {
            return 0;
        }
        //如果不存在就返回插入失败
        if (articleColumnService.selectArticleColumnById(Long.parseLong(param.getArticleColumnId())) == null) {
            return 0;
        }
        //唯一性验证 2 代表存在
        if (articleColumnAppExtensionService.checkColumnAppList(param.getLang(), param.getAppNumber(), param.getArticleColumnId(), param.getColumnAppId()) != 0) {
            return 2;
        }
        articleColumnApp.setId(Long.parseLong(param.getColumnAppId()));
        articleColumnApp.setLang(param.getLang().toUpperCase());
        //设置文章栏目app,公司信息
        CompanyAPPInfo companyAPPInfo = companyServiceRpc.selectCompanyAppInfo(param.getAppNumber());
        articleColumnApp.setAppNumber(param.getAppNumber());
        articleColumnApp.setAppName(param.getAppName());
        articleColumnApp.setAppId(companyAPPInfo.getAppId());
        articleColumnApp.setCompanyId(companyAPPInfo.getCompanyId());
        articleColumnApp.setCompanyNumber(companyAPPInfo.getCompanyNumber());
        articleColumnApp.setCompanyId(companyAPPInfo.getCompanyId());
        //设置公司级别信息
        articleColumnApp.setArticleColumnId(Long.parseLong(param.getArticleColumnId()));
        articleColumnApp.setColumnName(param.getColumnName());
        articleColumnApp.setDescription(param.getDescription());
        articleColumnApp.setSort(param.getSort());
        articleColumnApp.setUpdateTime(System.currentTimeMillis() / 1000);
        articleColumnApp.setStatus(param.getStatus());
        articleColumnApp.setSort(param.getSort());
        int success =  articleColumnAppMapper.updateByPrimaryKeySelective(articleColumnApp);
        //修改热门文章缓存
        articleExtensionService.updateHotArticleCachePo(articleColumnAppOne.getLang(),articleColumnAppOne.getAppNumber());
        //修改今日文章推荐缓存
        articleExtensionService.updateTodayArticleCachePo(articleColumnAppOne.getLang(),articleColumnAppOne.getAppNumber());
        return success;

    }

    @Override
    public int deleteArticleColumnAppList(String articleColumnAppIds) {
        List<String> columnIdList = Arrays.asList(articleColumnAppIds.split(","));
        int count = 0;
        for (String articleColumnAppId : columnIdList) {
            count += deleteArticleColumnApp(Long.parseLong(articleColumnAppId));
        }
        if (count == columnIdList.size()) {
            return 1;
        }
        return 0;
    }

}
